package com.bestcem.xm.ticket.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.bestcem.xm.common.core.domain.web.ServiceResult;
import com.bestcem.xm.ticket.controller.param.PageParam;
import com.bestcem.xm.ticket.controller.v2.vo.FilterInfoVO;
import com.bestcem.xm.ticket.controller.vo.IdAndTitleVO;
import com.bestcem.xm.ticket.controller.vo.SimpleSurveyVO;
import com.bestcem.xm.ticket.dao.TicketConfigDao;
import com.bestcem.xm.ticket.entity.mongo.TicketConfig;
import com.bestcem.xm.ticket.grpc.client.TicketBaseQdesGrpcService;
import com.bestcem.xm.ticket.grpc.client.dto.qdes.QdesDTO;
import com.bestcem.xm.ticket.service.FilterService;
import com.bestcem.xm.ticket.service.dto.PageDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.*;

/**
 * 工单中所有的下拉框数据获取
 *
 * @author zq.ding <zq.ding@idiaoyan.com>
 * @author XiaoYunSong <ys.xiao@idiaoyan.com> 2021-12-17 工单对接企业微信新增搜索功能
 * @version WechatWork
 * @date 2021/8/24 13:25
 */
@Slf4j
@Service
public class FilterServiceImpl implements FilterService {

    @Resource
    private TicketConfigDao ticketConfigDao;

    @Resource
    private TicketBaseQdesGrpcService ticketBaseQdesGrpcService;

    /**
     * 分页获取工单规则筛选条件选项
     *
     * @param orgId     公司id
     * @param pageParam 分页参数
     * @return com.bestcem.xm.util.ServiceResult<com.bestcem.xm.ticket.service.dto.PageDTO>
     * @author zq.ding <zq.ding@idiaoyan.com>
     * @date 2021/9/1 18:16
     */
    @Override
    public ServiceResult<PageDTO<IdAndTitleVO>> ruleFilter(String orgId, PageParam pageParam) {
        // 1、根据orgId和分页参数获取TicketConfig分页结果
        PageDTO<TicketConfig> pageDTO = ticketConfigDao.pageTicketConfig(orgId, pageParam);
        // 2、对返回的结果进行处理
        List<TicketConfig> ticketConfigList = pageDTO.getData();
        PageDTO<IdAndTitleVO> result = new PageDTO<>();
        result.setPage(pageDTO.getPage());
        result.setTotal(pageDTO.getTotal());
        // 3、初始化数组
        List<IdAndTitleVO> list = new ArrayList<>(ticketConfigList.size());

        // 4、判断集合是否为空
        if (CollectionUtil.isNotEmpty(ticketConfigList)) {
            ticketConfigList.forEach(o -> {
                // 5、构建需要的结果并放入数组中
                list.add(new IdAndTitleVO(o.getTitle(), o.getId()));
            });
            result.setData(list);
        } else {
            result.setData(new ArrayList<>());
        }

        return ServiceResult.success(result);
    }


    /**
     * 分页获取工单规则筛选条件选项, 支持搜索
     *
     * @param orgId     公司id
     * @param pageParam 分页参数
     * @param search    搜索关键词
     * @return 分页/搜索 结果
     * @author XiaoYunSong <ys.xiao@idiaoyan.com>
     * @date 2021/12/17 14:42
     */
    @Override
    public ServiceResult<PageDTO<FilterInfoVO>> ruleFilter(String orgId, PageParam pageParam, String search) {
        // 根据 orgId,搜索关键词,分页参数 获取TicketConfig分页结果
        PageDTO<TicketConfig> pageDTO = ticketConfigDao.searchAndPage(orgId, search, pageParam);
        // 对返回的结果进行处理
        List<TicketConfig> ticketConfigList = pageDTO.getData();
        PageDTO<FilterInfoVO> result = new PageDTO<>();
        result.setPage(pageDTO.getPage());
        result.setTotal(pageDTO.getTotal());
        List<FilterInfoVO> list = new ArrayList<>();
        if (!CollectionUtils.isEmpty(ticketConfigList)) {
            for (TicketConfig ticketConfig : ticketConfigList) {
                list.add(new FilterInfoVO(ticketConfig.getId(), ticketConfig.getTitle()));
            }
        }
        result.setData(list);
        return ServiceResult.success(result);
    }


    /**
     * 分页获取问卷项目筛选条件选项
     *
     * @param orgId     公司id
     * @param pageParam 分页参数
     * @return com.bestcem.xm.util.ServiceResult<com.bestcem.xm.ticket.service.dto.PageDTO>
     * @author zq.ding <zq.ding@idiaoyan.com>
     * @date 2021/9/1 18:17
     */
    @Override
    public ServiceResult<PageDTO<SimpleSurveyVO>> surveyFilter(String orgId, PageParam pageParam) {
        LinkedHashSet<String> allSurveyId = ticketConfigDao.groupSurveyId(orgId);

        // 返回给上一层的PageDTO
        PageDTO<SimpleSurveyVO> pageDTO = new PageDTO<>();
        pageDTO.setTotal(0L);
        pageDTO.setPage(pageParam.getPage());
        pageDTO.setData(new ArrayList<>());
        if (CollectionUtils.isEmpty(allSurveyId)) {
            return ServiceResult.success(pageDTO);
        }

        pageDTO.setTotal((long) allSurveyId.size());

        List<String> surveyIdList = new ArrayList<>();
        int start = pageParam.getSize() * (pageParam.getPage() - 1),
                end = Math.min(pageParam.getSize() * pageParam.getPage(), allSurveyId.size());
        int i = 0;
        for (String surveyId : allSurveyId) {
            if (start <= i && i < end) {
                surveyIdList.add(surveyId);
            }
            i++;
        }

        // 存放最终结果的列表
        List<QdesDTO> qdesDTOList = ticketBaseQdesGrpcService.listQdesProjectByIds(surveyIdList);
        if (!CollectionUtils.isEmpty(qdesDTOList)) {
            List<SimpleSurveyVO> result = new ArrayList<>(end);
            Map<String, String> surveyMap = new HashMap<>();
            for (QdesDTO qdesDTO : qdesDTOList) {
                surveyMap.put(qdesDTO.getId(), qdesDTO.getTitle());
            }
            for (String surveyId : surveyIdList) {
                String surveyTitle = surveyMap.get(surveyId);
                if (ObjectUtils.isEmpty(surveyTitle)) {
                    surveyTitle = "问卷不存在";
                }
                result.add(new SimpleSurveyVO(surveyTitle, surveyId));
            }
            pageDTO.setData(result);
        }

        return ServiceResult.success(pageDTO);
    }


    /**
     * 分页获取问卷项目筛选条件选项, 支持搜索
     *
     * @param orgId     公司id
     * @param pageParam 分页参数
     * @param search    搜索关键词
     * @return 分页/搜索 结果
     * @author XiaoYunSong <ys.xiao@idiaoyan.com>
     * @date 2021/12/17 14:41
     */
    @Override
    public ServiceResult<PageDTO<FilterInfoVO>> qdesFilter(String orgId, PageParam pageParam, String search) {
        // TODO: 逻辑
        return null;
    }

}
